package com.zdy.im.tio.command;

import com.sixeco.framework.util.json.JsonUtils;
import com.zdy.im.chat.ChatMessageInfo;
import com.zdy.im.common.exception.ImException;
import com.zdy.im.core.ImConst;
import com.zdy.im.tio.TioServerConfig;
import com.zdy.im.tio.packets.SocketResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.http.common.HttpRequest;
import org.tio.utils.lock.SetWithLock;
import org.tio.websocket.common.WsRequest;
import org.tio.websocket.common.WsResponse;
import org.tio.websocket.common.WsSessionContext;

/**
 * @Author 作者 : zdy
 * @CreateDate 创建时间: 2021-02-30
 * @Version 版本: v1.0.0
 * <pre>
 *
 *  @Description (说明):  功能协议抽象类
 *
 * </pre>
 **/

public abstract class AbstractCommandHandler implements CmdHandler, ImConst {

    @Autowired
    private CommandHandlerManager commandManager;


    public WsSessionContext getSessionContext(ChannelContext channelContext){
        return (WsSessionContext)channelContext.getAttribute(Key.IM_CHANNEL_CONTEXT_KEY);
    }

    public HttpRequest getHandshakeRequest(ChannelContext channelContext){
        WsSessionContext wsSessionContext = getSessionContext(channelContext);
        if(wsSessionContext == null){
            return null;
        }

        HttpRequest request = wsSessionContext.getHandshakeRequest();
        return request;
    }



    public WsResponse converterWsResponse(SocketResponseVo response){
        String json = JsonUtils.toJson(response);
        WsResponse wsResponse = WsResponse.fromText(json, TioServerConfig.CHARSET);
        return wsResponse;
    }


    public ChatMessageInfo converterWsParam(WsRequest packet, ChannelContext channelContext) throws ImException{
        if (packet.getBody() == null) {
            throw new ImException("body is null");
        }
        ChatMessageInfo sendInfo = JsonUtils.parseObject(packet.getWsBodyText(), ChatMessageInfo.class);
        if (sendInfo == null || sendInfo.getExt() == null ) {
            return null;
        }
        return sendInfo;
    }
    /**
     * 判断用户是否在线
     * @param userId 用户ID
     * @param channelContext 是否开启持久化(true:开启,false:未开启)
     * @return
     */
    public boolean isOnline( ChannelContext channelContext,String userId ){

        SetWithLock<ChannelContext> channelContextSetWithLock = Tio.getByUserid(channelContext.getTioConfig(), userId);
        if( null != channelContextSetWithLock  &&
                null != channelContextSetWithLock.getObj()
                && !channelContextSetWithLock.getObj().isEmpty()) {
            return true;
        }
        return false;
    }

    protected String getOssImgResize(String sourcePath,String ossReSize){
        if(null == sourcePath || "".equals(sourcePath)){
            return "";
        }
        if(null == ossReSize || "".equals(ossReSize)){
            return sourcePath;
        }
        if(sourcePath.contains("?")){
            return sourcePath;
        }
        return sourcePath+ossReSize;
    }

}
